package com.enoram.gwt.desktop.client.ui;

import com.enoram.gwt.desktop.client.ui.style.Styles;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DeskItem extends Composite{
	private ImageResource imageRes; 
	private Panel outer = new VerticalPanel();
	private DecoratorPanel panel = new DecoratorPanel();
	private Anchor link = new Anchor();
	private Label text = new Label();
	private Token token;
	
	private ClickHandler clickHandler;
	public DeskItem(){
		initWidget(outer);
		outer.add(panel);
		outer.add(link);
		text.setStyleName("desktop-item-text");
		
		
		
	}
	
	public DeskItem(String text, ImageResource resource, Token token){
		this();
		setImage(resource);
		setText(text);
		
		this.token = token;
		
	}
	
	public DeskItem(String text, ImageResource resource, ClickHandler clickHandler){
		this();
		this.clickHandler = clickHandler;
		setImage(resource);
		setText(text);
		
		
		
	}
	
	public void setText(String text){
		this.text.setText(text);
		
		link.setText(text);
		if(clickHandler!=null){
			link.addClickHandler(clickHandler);
			link.setStyleName(Styles.common().desktopActive());
		}
		
	}
	
	public void setImage(ImageResource resource){
		final Image image = new Image(resource);
		panel.add(image);
		
		
		FlexTable table = new FlexTable();
		
		
		image.addMouseOverHandler(new MouseOverHandler() {
			
			@Override
			public void onMouseOver(MouseOverEvent event) {
				
			//	Window.alert("test");
				
			}
		});
		if(token!=null){
			image.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(token.getUri());
					
				}
			});
		}
		if(clickHandler!=null){
			image.addClickHandler(clickHandler);
			image.setStyleName(Styles.common().desktopActive());
		}
		
	}
	
}
